const { v4: uuidv4 } = require('uuid');

module.exports = {
  up: async (queryInterface) => {
    const now = new Date();
    
    const permissions = [
      // ========== 用户管理模块 ==========
      {
        id: uuidv4(),
        resource: 'user',
        action: 'create',
        description: '创建用户',
        module: 'user_management',
        module_name: '用户管理',
        is_system: false,
        sort_order: 10,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        resource: 'user',
        action: 'read',
        description: '查看用户',
        module: 'user_management',
        module_name: '用户管理',
        is_system: false,
        sort_order: 11,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        resource: 'user',
        action: 'update',
        description: '更新用户',
        module: 'user_management',
        module_name: '用户管理',
        is_system: false,
        sort_order: 12,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        resource: 'user',
        action: 'delete',
        description: '删除用户',
        module: 'user_management',
        module_name: '用户管理',
        is_system: false,
        sort_order: 13,
        created_at: now,
        updated_at: now,
      },

      // ========== 角色管理模块 ==========
      {
        id: uuidv4(),
        resource: 'role',
        action: 'create',
        description: '创建角色',
        module: 'role_management',
        module_name: '角色管理',
        is_system: false,
        sort_order: 20,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        resource: 'role',
        action: 'read',
        description: '查看角色',
        module: 'role_management',
        module_name: '角色管理',
        is_system: false,
        sort_order: 21,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        resource: 'role',
        action: 'update',
        description: '更新角色',
        module: 'role_management',
        module_name: '角色管理',
        is_system: false,
        sort_order: 22,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        resource: 'role',
        action: 'delete',
        description: '删除角色',
        module: 'role_management',
        module_name: '角色管理',
        is_system: false,
        sort_order: 23,
        created_at: now,
        updated_at: now,
      },

      // ========== 菜单管理模块 ==========
      {
        id: uuidv4(),
        resource: 'menu',
        action: 'create',
        description: '创建菜单',
        module: 'menu_management',
        module_name: '菜单管理',
        is_system: false,
        sort_order: 30,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        resource: 'menu',
        action: 'read',
        description: '查看菜单',
        module: 'menu_management',
        module_name: '菜单管理',
        is_system: false,
        sort_order: 31,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        resource: 'menu',
        action: 'update',
        description: '更新菜单',
        module: 'menu_management',
        module_name: '菜单管理',
        is_system: false,
        sort_order: 32,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        resource: 'menu',
        action: 'delete',
        description: '删除菜单',
        module: 'menu_management',
        module_name: '菜单管理',
        is_system: false,
        sort_order: 33,
        created_at: now,
        updated_at: now,
      },

      // ========== 审计日志模块 ==========
      {
        id: uuidv4(),
        resource: 'audit_log',
        action: 'read',
        description: '查看审计日志',
        module: 'audit_management',
        module_name: '审计管理',
        is_system: false,
        sort_order: 40,
        created_at: now,
        updated_at: now,
      },

      // ========== 字典管理模块 ==========
      {
        id: uuidv4(),
        resource: 'dictionary',
        action: 'create',
        description: '创建字典',
        module: 'dictionary_management',
        module_name: '字典管理',
        is_system: false,
        sort_order: 50,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        resource: 'dictionary',
        action: 'read',
        description: '查看字典',
        module: 'dictionary_management',
        module_name: '字典管理',
        is_system: false,
        sort_order: 51,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        resource: 'dictionary',
        action: 'update',
        description: '更新字典',
        module: 'dictionary_management',
        module_name: '字典管理',
        is_system: false,
        sort_order: 52,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        resource: 'dictionary',
        action: 'delete',
        description: '删除字典',
        module: 'dictionary_management',
        module_name: '字典管理',
        is_system: false,
        sort_order: 53,
        created_at: now,
        updated_at: now,
      },

      // ========== 租户管理模块（系统级权限） ==========
      {
        id: uuidv4(),
        resource: 'tenant',
        action: 'create',
        description: '创建租户',
        module: 'tenant_management',
        module_name: '租户管理',
        is_system: true,
        sort_order: 100,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        resource: 'tenant',
        action: 'read',
        description: '查看租户',
        module: 'tenant_management',
        module_name: '租户管理',
        is_system: true,
        sort_order: 101,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        resource: 'tenant',
        action: 'update',
        description: '更新租户',
        module: 'tenant_management',
        module_name: '租户管理',
        is_system: true,
        sort_order: 102,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        resource: 'tenant',
        action: 'delete',
        description: '删除租户',
        module: 'tenant_management',
        module_name: '租户管理',
        is_system: true,
        sort_order: 103,
        created_at: now,
        updated_at: now,
      },

      // ========== 套餐管理模块（系统级权限） ==========
      {
        id: uuidv4(),
        resource: 'plan',
        action: 'create',
        description: '创建套餐',
        module: 'plan_management',
        module_name: '套餐管理',
        is_system: true,
        sort_order: 110,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        resource: 'plan',
        action: 'read',
        description: '查看套餐',
        module: 'plan_management',
        module_name: '套餐管理',
        is_system: true,
        sort_order: 111,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        resource: 'plan',
        action: 'update',
        description: '更新套餐',
        module: 'plan_management',
        module_name: '套餐管理',
        is_system: true,
        sort_order: 112,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        resource: 'plan',
        action: 'delete',
        description: '删除套餐',
        module: 'plan_management',
        module_name: '套餐管理',
        is_system: true,
        sort_order: 113,
        created_at: now,
        updated_at: now,
      },
    ];

    await queryInterface.bulkInsert('permissions', permissions);
  },

  down: async (queryInterface) => {
    await queryInterface.bulkDelete('permissions', null, {});
  },
};

